import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import java.awt.event.*;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.Timer;


/**
 * The Battle mini-game controller class
 * @author michaeldunn
 *@version 1.0
 */

public class BattleController extends JPanel{
	
	
	private Player user;
	private Inventory playerInventory;
	private JFrame frame;
	
	private final int LIFE = 3;
	private double lossPercent;
	
	private int shots = 10;
	private int cX, cY, mouseX, mouseY, hits, timeDelayCheck;
	private ImageIcon img = new ImageIcon("Images/Enemy Ship.jpg");
	private Image ship;
	private PanelUpdater updater;
	private boolean battling = true;
	
	
	public BattleController(Player user, Inventory playerInventory, JFrame frame){
		setPreferredSize(new Dimension(600,425));
		hits = 0;
		timeDelayCheck = 0;
		
		this.user = user;
		this.playerInventory = playerInventory;
		this.frame = frame;
		
		lossPercent = (10.0 - user.getFighting()) / 100;
		
		ship = img.getImage();
		ship = ship.getScaledInstance(ship.getHeight(getParent())/3, 
				ship.getWidth(getParent())/3, 0);
		
		updater = new PanelUpdater(this, 45);
		updater.start();
		
		this.setBackground(Color.black);
		
		addMouseListener(new MouseEventListener());
		addMouseMotionListener(new MouseMotion());
	}
	
	public void updateTarget(){
		if(timeDelayCheck%20 == 0){
				cX = (int)(Math.random()*450+100); 
 				cY = (int)(Math.random()*300+100); 
		}
		timeDelayCheck++;
	}
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		g.setColor(Color.GREEN);

		
		g.drawImage(ship, cX - ship.getWidth(getParent()), 
				cY - ship.getHeight(getParent())+100, null);
		
		g.drawString("Shots Left: " + shots, 100, 425);
		g.drawString("Hits: " + hits + " of 3", 400, 425);

		
		
		//draw cursor
		g.fillRect(mouseX-8, mouseY, 20, 3);
		g.fillRect(mouseX, mouseY-8, 3, 20);
		
		g.fillRect(mouseX-10, mouseY - 10, 3, 6);
		g.fillRect(mouseX-10, mouseY - 10, 6, 3);
		
		g.fillRect(mouseX+10, mouseY - 10, 3, 6);
		g.fillRect(mouseX+6, mouseY - 10, 6, 3);
		
		g.fillRect(mouseX-10, mouseY+6, 3, 6);
		g.fillRect(mouseX-10, mouseY+10, 6, 3);
		
		g.fillRect(mouseX+7, mouseY+10, 6, 3);
		g.fillRect(mouseX+10, mouseY+7, 3, 6);
	}
	
	public boolean isBattling(){
		return battling;
	}
	
	public int getLifeLeft(){
		return LIFE - hits;
	}

	private class PanelUpdater implements ActionListener{
		private Timer timer;
		private BattleController update;
		
		public PanelUpdater(BattleController update, int secondsInterval){
			timer = new Timer(secondsInterval, this);
			this.update = update;
		}
		
		public void start(){
			timer.start();
		}
		public void stop(){
			timer.stop();
		}
		public void actionPerformed(ActionEvent event) {
				this.update.updateTarget();
				this.update.repaint();
		}
	}
	private class MouseMotion implements MouseMotionListener{
		public void mouseMoved(MouseEvent e){
			
			mouseX = e.getX();
			mouseY = e.getY();
			
		}
		public void mouseDragged(MouseEvent e){}
	}
private class MouseEventListener implements MouseListener{
		
		public void mouseClicked(MouseEvent e){
			
			shots--;
			
			if(Math.abs((double)mouseX - cX) < ship.getWidth(getParent())
				&& Math.abs((double)mouseY - cY) < ship.getHeight(getParent())){
				hits ++;
			}
			if(hits == LIFE){
				user.getInventory().setMoney((long)(
				user.getInventory().getMoney() + 
				user.getInventory().getMoney()*lossPercent));
				JOptionPane.showMessageDialog(
						frame, "You defeated some pirates!" +
						" You gained some money.");
				battling = false;
			}else if(shots == 0){
				user.getInventory().setMoney((long)(
				user.getInventory().getMoney() - 
				user.getInventory().getMoney()*lossPercent));
				JOptionPane.showMessageDialog(
						frame, "You were defeated by pirates!" +
						" You lost some money.");
				battling = false;
			}
			repaint();
			
		}	
		
				
		public void mousePressed(MouseEvent e){}
		public void mouseReleased(MouseEvent e){}
		public void mouseHover(MouseEvent e){}
		public void mouseExited(MouseEvent e){}
		public void mouseEntered(MouseEvent e){}
		
	}


}
